package com.dong.ui.view;

import com.dong.common.view.CommonView;
import com.dong.ui.component.CommonFrame;
import com.dong.ui.component.IconPanel;
import com.dong.ui.component.home.FoodListPanel;
import com.dong.ui.component.home.TopPanel;
import com.dong.ui.constant.IconConst;
import com.dong.ui.constant.RouterConst;
import com.dong.ui.util.RouterUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: 陈瑞东
 * @create: 2021-08-26 14:52
 */
public class HomeFrame extends CommonFrame implements CommonView {

    JFrame jf = this;

    /*==================组件==================*/
    private static final int WIDTH = 848;
    private static final int HEIGHT = 600;

    private TopPanel topPanel; // 上方容器

    private FoodListPanel bottomPanel; // 下方容器


    /*==================组件==================*/
    /*==================参数==================*/


    /*==================参数==================*/
    /*==================标志==================*/

    /*==================标志==================*/


    @Override
    public void init(Map<String, Object> params) {
        // 设置标题
        setTitle("主页");


        handleView();

        // 设置大小
        setSize(new Dimension(WIDTH, HEIGHT));
        // 设置可以改变大小
        setResizable(true);
        // 设置居中
        setLocationRelativeTo(null);
        // 设置可见
        setVisible(true);

        handleAction();
    }

    @Override
    public void initComponent() {

    }

    @Override
    public void initParams() {

    }

    @Override
    public void handleView() {
        initParams();
        initComponent();

        // 背景容器
        JPanel bgPabel = new JPanel(new BorderLayout());

        // ===========================顶部容器===========================

        // 顶部容器——用于隔开顶部距离
        Box topVBox = Box.createVerticalBox();
        // 顶部容器——包含控件
        topPanel = new TopPanel(new FlowLayout(FlowLayout.LEFT));

        topVBox.add(Box.createVerticalStrut(1));
        topVBox.add(topPanel);
//        topVBox.add(Box.createVerticalStrut(20));

        // ===========================顶部容器===========================

        // 底部容器
        bottomPanel = new FoodListPanel(new BorderLayout());



        bgPabel.add(topVBox, BorderLayout.NORTH);
        bgPabel.add(bottomPanel);

        // 添加背景容器至窗体
        this.add(bgPabel);
    }

    @Override
    public void handleAction() {
        // 点击上方容器中的图标事件
        topIconClickEvent();
    }

    /**
     * 点击上方容器图标事件
     */
    private void topIconClickEvent() {
        topPanel.addCompomentMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() != MouseEvent.BUTTON1) {
                    return;
                }
                Object source = e.getSource();
                if (source instanceof IconPanel) {
                    switch (((IconPanel) source).getText()) {
                        case IconConst.ICON_ADDNEW:
                            Map<String, Object> params = new HashMap<String, Object>() {{
                                put("parentPanel", bottomPanel);
                            }};
                            RouterUtils.router(RouterConst.VIEW_ADDFOOD, params);
                            break;
                        case IconConst.ICON_FOODLIST:
                            System.out.println(IconConst.ICON_FOODLIST);
                            break;
                        default:
                            System.out.println("default");
                            break;
                    }
                }
            }
        });
    }
}
